import { Service } from 'egg';

/**
 * Test Service
 */
export default class Test extends Service {

  /**
   * 注册
   * @param name, password
   */
  public async register(_name, _password, _token, _app) {
    const { app, ctx } = this;
    const req = await app.mysql.insert('user', { name: _name, password: _password, token: _token });
    if (req.affectedRows) {
      return ctx.response.msg(200, 200, '新增用户成功', []);
    }
  }
  /**
  * 登录
  * @param name, password
  */
  public async login(_name, _password, _token, _app) {
    const { app, ctx } = this;
    const req = await app.mysql.query('SELECT id,name,token,password FROM `user` WHERE `name` =?', [_name])
    if (req.length) {
      console.log(ctx.helper.Jsonsql(req)[0])
      if (ctx.helper.Jsonsql(req)[0].password == _password) {
        return ctx.response.msg(200, 200, '登录成功', ctx.helper.Jsonsql(req));
      } else {
        return ctx.response.msg(200, 305, '密码错误', []);
      }
    } else {
      return ctx.response.msg(200, 305, '账户错误', []);
    }
  }
  /**
  * 查询用户信息
  * @param userID
  */
  public async userinfo(_userID) {
    const { app, ctx } = this;
    const userinfo = await app.mysql.query('SELECT id,name,password,age,phone FROM `user` WHERE `id` =?', [_userID])
    return ctx.response.msg(200, 200, '成功', ctx.helper.Jsonsql(userinfo));
  }
}

